package com.yunche.yunchebusiness.ui.activity.evaluate

import android.graphics.Color
import android.support.design.widget.TabLayout
import android.view.View
import android.widget.TextView
import com.alibaba.fastjson.JSONObject
import com.bumptech.glide.Glide
import com.yunche.yunchebusiness.R
import com.yunche.yunchebusiness.base.BaseActivity
import com.yunche.yunchebusiness.common.CommonDialog
import com.yunche.yunchebusiness.common.Constant
import com.yunche.yunchebusiness.entity.*
import com.yunche.yunchebusiness.mvp.contract.EvaluateContract
import com.yunche.yunchebusiness.mvp.presenter.EvaluatePresenter
import com.yunche.yunchebusiness.toRequestBody
import com.yunche.yunchebusiness.ui.view.YuncheToolBar
import com.yunche.yunchebusiness.utils.ToastyUtils
import kotlinx.android.synthetic.main.activity_evaluate_info.*
import org.jetbrains.anko.backgroundResource
import org.jetbrains.anko.textColor

class EvaluateInfoActivity : BaseActivity(), View.OnClickListener, EvaluateContract.View {

    val PriceTypeLow = 1
    val PriceTypeMiddle = 2
    val PriceTypeHigh = 3
    var evaluateInfo: EvaluateInfo? = null
    var evaluateEntity: EvaluateEntity? = null

    var priceEntity: C2BPrices? = null

    var B2C = 5
    var C2B = 6

    var priceType = B2C

    val mPresenter by lazy {
        EvaluatePresenter()
    }

    override fun setLayout(): Int = R.layout.activity_evaluate_info

    override fun initView() {
        mPresenter.attachView(this)
        tab_layout.addTab(tab_layout.newTab().setText("车商品收购价"))
        tab_layout.addTab(tab_layout.newTab().setText("车商销售价格"))
    }

    override fun getColorSuccess(colorEntity: ColorEntity) {
    }

    override fun getEvaluateSuccess(evaluateEntity: EvaluateEntity) {
        this.evaluateEntity = evaluateEntity
        priceEntity = evaluateEntity.b2CPrices

        tv_price_low.text = evaluateEntity.b2CPrices.a.low + "万元"
        tv_price_middle.text = evaluateEntity.b2CPrices.a.mid + "万元"
        tv_price_high.text = evaluateEntity.b2CPrices.a.up + "万元"

        tv_new_car_price.text = evaluateEntity.ncmsrp + "万元"


        var para = JSONObject()
        para["bandCode"] = evaluateInfo?.bandCode
        para["maxEvaluate"] = evaluateEntity.b2CPrices.a.up
        para["minEvaluate"] = evaluateEntity.b2CPrices.c.low
        para["modelCode"] = evaluateInfo?.modelCode
        para["seriesCode"] = evaluateInfo?.seriesCode
        para["cityName"] = evaluateInfo?.cityName
        para["mileage"] = evaluateInfo?.mileage!!.toFloat() * 10000
        para["buyCarDate"] = evaluateInfo?.buyCarDate
        if (evaluateInfo?.id != -1) {
            para["id"] = evaluateInfo?.id
        }
        mPresenter.saveEvaluate(para.toJSONString().toRequestBody())
    }

    override fun getAreaSuccess(areas: List<AreaEntity>) {
    }

    override fun saveEvaluateSuccess() {
        ToastyUtils.show("提交成功")
    }

    override fun showLoading() {
        CommonDialog.showLoading(mContext!!, "加载中")
    }

    override fun hideLoading() {
        CommonDialog.cancle()
    }

    override fun showError(msg: String) {
        ToastyUtils.showError(msg)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_price_type_a -> {
                switchType(PriceTypeHigh)
            }
            R.id.tv_price_type_b -> {
                switchType(PriceTypeMiddle)
            }
            R.id.tv_price_type_c -> {
                switchType(PriceTypeLow)
            }
        }
    }

    override fun initData() {
        var bundle = intent.extras
        if (bundle != null) {

            evaluateInfo = bundle.getSerializable("evaluateInfo") as EvaluateInfo
            tv_model_name.text = evaluateInfo?.brandName + "-" + evaluateInfo?.modelName
            Glide.with(mContext!!).load(evaluateInfo?.icon!!).into(iv_series_cover)

            tv_evaluate_city.text = evaluateInfo?.cityName
            tv_plate_time.text = evaluateInfo?.buyCarDate
            tv_mileage.text = evaluateInfo?.mileage + "万公里"


            var para = JSONObject()
            para["mileage"] = evaluateInfo?.mileage!!.toFloat() * 10000
            para["buyCarDate"] = evaluateInfo?.buyCarDate
            para["trimId"] = evaluateInfo?.modelCode
            para["cityId"] = evaluateInfo?.cityName

            mPresenter.getEvaluate(para.toJSONString().toRequestBody())

        } else {
            ToastyUtils.showError("数据有误")
            finish()
        }
    }

    override fun onBackPressed() {
        setResult(Constant.REFRESH_DATA)
        finish()
    }

    override fun initEvent() {
        tv_price_type_a.setOnClickListener(this)
        tv_price_type_b.setOnClickListener(this)
        tv_price_type_c.setOnClickListener(this)

        toolbar.yuncheToolbarCallBack = object : YuncheToolBar.YuncheToolbarCallBack {
            override fun onBackClick() {
                onBackPressed()
            }

            override fun onRightClick() {
            }
        }

        tab_layout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                switchType(PriceTypeHigh)
                priceEntity = if (tab?.position == 0) evaluateEntity?.c2BPrices else evaluateEntity?.b2CPrices
            }

        })

    }

    private fun switchType(type: Int) {


        when (type) {
            PriceTypeHigh -> {
                changeSelect(tv_price_type_a)
                changeNormal(tv_price_type_b)
                changeNormal(tv_price_type_c)
                tv_price_low.text = priceEntity?.a?.low + "万元"
                tv_price_middle.text = priceEntity?.a?.mid + "万元"
                tv_price_high.text = priceEntity?.a?.up + "万元"
            }
            PriceTypeLow -> {
                changeNormal(tv_price_type_a)
                changeNormal(tv_price_type_b)
                changeSelect(tv_price_type_c)
                tv_price_low.text = priceEntity?.c?.low + "万元"
                tv_price_middle.text = priceEntity?.c?.mid + "万元"
                tv_price_high.text = priceEntity?.c?.up + "万元"
            }

            PriceTypeMiddle -> {
                changeNormal(tv_price_type_a)
                changeSelect(tv_price_type_b)
                changeNormal(tv_price_type_c)
                tv_price_low.text = priceEntity?.b?.low + "万元"
                tv_price_middle.text = priceEntity?.b?.mid + "万元"
                tv_price_high.text = priceEntity?.b?.up + "万元"
            }
        }
    }

    private fun changeSelect(tv: TextView) {
        tv.backgroundResource = R.drawable.btn_bg_blue
        tv.textColor = Color.parseColor("#ffffff")
    }

    private fun changeNormal(tv: TextView) {
        tv.backgroundResource = R.drawable.btn_bg_grey
        tv.textColor = Color.parseColor("#B4B4B5")
    }

}
